define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'account/record/index' + location.search,
                    add_url: 'account/record/add',
                    accountrecord_url: 'account/record/accountrecord',
                    smallaccountrecord_url: 'account/record/smallaccountrecord',
                    borrowaccountrecord_url: 'account/record/borrowaccountrecord',
                    orderaccountrecord_url: 'account/record/orderaccountrecord',
                    del_url: 'account/record/del',
                    multi_url: 'account/record/multi',
                    import_url: 'account/record/import',
                    table: 'order_account_record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'oar_id',
                sortName: 'oar_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'oar_out_account_title', title: __('Oar_out_account_title'), operate: 'LIKE'},
                        {field: 'oar_in_account_title', title: __('Oar_in_account_title'), operate: 'LIKE'},
                        {field: 'oar_money', title: __('Oar_money'), operate:'BETWEEN'},
                        {field: 'oar_type', title: __('Oar_type'), searchList: {"1":__('Oar_type 1'),"2":__('Oar_type 2'),"3":__('Oar_type 3'),"4":__('Oar_type 4'),"5":__('Oar_type 5')}, formatter: Table.api.formatter.normal},
                        {field: 'oar_account_type', title: __('Oar_account_type'), searchList: {"0":'无',"1":__('Oar_account_type 1'),"2":__('Oar_account_type 2'),"3":__('Oar_account_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'oar_status', title: __('Oar_status'), searchList: {"0":__('Oar_status 0'),"1":__('Oar_status 1'),"2":__('Oar_status 2')}, formatter: Table.api.formatter.status},
                        {field: 'oar_note', title: __('Oar_note'), operate: 'LIKE'},
                        {field: 'oar_create_time', title: '创建时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $(document).on("click", ".btn-accountrecord", function (e) {
                // 获取选中的列表ID
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.accountrecord_url + "?ids=" + ids, '批量修改')
            });
            $(document).on("click", ".btn-smallaccountrecord", function (e) {
                // 获取选中的列表ID
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.smallaccountrecord_url + "?ids=" + ids, '批量修改')
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        accountrecord: function () {
            Controller.api.bindevent();
        },
        orderaccountrecord: function () {
            Controller.api.bindevent();
        },
        borrowaccountrecord: function () {
            Controller.api.bindevent();
        },
        smallaccountrecord: function () {
            // 切换单/多规格
            $('select[name="row[status]"]').change(function (e) {
                var $out        = $('.out')
                    , $in      = $('.in');

                if (e.currentTarget.value === '2') {
                    $out.show() && $in.hide();
                }
                if(e.currentTarget.value === '1') {
                    $in.show() && $out.hide();
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
